package org.jeecg.modules.lab.items.relve.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.jeecg.modules.lab.items.entity.LabItemsBasisIndex;
import org.jeecg.modules.lab.items.entity.LabItemsRelevBasis;
import org.jeecg.modules.lab.items.item.service.ILabItemsBasisIndexService;
import org.jeecg.modules.lab.items.relve.service.ILabItemsRelevBasisService;
import org.jeecg.modules.lab.items.vo.LabItemsRelevBasisPage;
import org.jeecg.modules.lab.items.vo.LabItemsRelevPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 项目与标准关联表
 * @Author: jeecg-boot
 * @Date: 2024-01-03
 * @Version: V1.0
 */
@Api(tags = "项目与标准关联表")
@RestController
@RequestMapping("/items/labItemsRelevBasis")
@Slf4j
public class LabItemsRelevBasisController extends JeecgController<LabItemsRelevBasis, ILabItemsRelevBasisService> {
    @Autowired
    private ILabItemsRelevBasisService labItemsRelevBasisService;

    @Autowired
    private ILabItemsBasisIndexService labItemsBasisIndexService;

    @Autowired
    private ILabCronJobService labCronJobService;

    /**
     * 分页列表查询
     *
     * @param labItemsRelevBasis
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "项目与标准关联表-分页列表查询")
    @ApiOperation(value = "项目与标准关联表-分页列表查询", notes = "项目与标准关联表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<LabItemsRelevBasis>> queryPageList(LabItemsRelevBasis labItemsRelevBasis,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                           HttpServletRequest req) {
        QueryWrapper<LabItemsRelevBasis> queryWrapper = QueryGenerator.initQueryWrapper(labItemsRelevBasis, req.getParameterMap());
        Page<LabItemsRelevBasis> page = new Page<LabItemsRelevBasis>(pageNo, pageSize);
        IPage<LabItemsRelevBasis> pageList = labItemsRelevBasisService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 更新项目id查询关联的标准和对应的实验指标
     *
     * @return
     */
    //@AutoLog(value = "项目与标准关联表-分页列表查询")
    @ApiOperation(value = "项目与标准关联表-分页列表查询", notes = "项目与标准关联表-分页列表查询")
    @GetMapping(value = "/queryItemsRelevPage")
    public Result<LabItemsRelevPage> queryItemsRelev(@RequestParam(name = "itemsId", defaultValue = "") String itemsId) {

        LabItemsRelevPage labItemsRelevPage = new LabItemsRelevPage();
        QueryWrapper<LabItemsRelevBasis> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("items_id", itemsId);
        List<LabItemsRelevBasis> labItemsRelevBasisLsit = labItemsRelevBasisService.list(queryWrapper);
        QueryWrapper<LabItemsBasisIndex> indexWrapper = new QueryWrapper<>();
        List<LabItemsRelevBasisPage> itemsRelevBasisPageList = new ArrayList<>();
        for (LabItemsRelevBasis itemsRelevBasis : labItemsRelevBasisLsit) {
            LabItemsRelevBasisPage relevBasisPage = new LabItemsRelevBasisPage();
            BeanUtils.copyProperties(itemsRelevBasis, relevBasisPage);
            indexWrapper.clear();
            indexWrapper.eq("items_id", itemsId);
            indexWrapper.eq("basis_id",itemsRelevBasis.getBasisId());
            relevBasisPage.setLabItemsBasisIndexList(labItemsBasisIndexService.list(indexWrapper));
            itemsRelevBasisPageList.add(relevBasisPage);
        }
        labItemsRelevPage.setLabItemsRelevBasisPageList(itemsRelevBasisPageList);
        return Result.OK(labItemsRelevPage);
    }

    /**
     * 添加
     *
     * @param labItemsRelevBasis
     * @return
     */
    @AutoLog(value = "项目与标准关联表-添加")
    @ApiOperation(value = "项目与标准关联表-添加", notes = "项目与标准关联表-添加")
//	@RequiresPermissions("labItemsRelevBasis:lab_items_relev_basis:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody LabItemsRelevBasis labItemsRelevBasis) {
        labItemsRelevBasisService.save(labItemsRelevBasis);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param labItemsRelevBasis
     * @return
     */
    @AutoLog(value = "项目与标准关联表-编辑")
    @ApiOperation(value = "项目与标准关联表-编辑", notes = "项目与标准关联表-编辑")
//	@RequiresPermissions("labItemsRelevBasis:lab_items_relev_basis:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody LabItemsRelevBasis labItemsRelevBasis) {
        labItemsRelevBasisService.updateById(labItemsRelevBasis);
        return Result.OK("编辑成功!");
    }

    /**
     * 保存或修改
     *
     * @return
     */
    @AutoLog(value = "项目与标准关联表-保存或修改")
    @ApiOperation(value = "项目与标准关联表-保存或修改", notes = "项目与标准关联表-保存或修改")
    @RequestMapping(value = "/saveOrUpdate", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> saveOrUpdate(@RequestBody LabItemsRelevPage labItemsRelevPage) {
        labItemsRelevBasisService.saveOrUpdate(labItemsRelevPage);


        if (MyEnum.sync) {
            //同步表信息
            String itemsId = labItemsRelevPage.getItemsId();
//            String basisId = labItemsRelevPage;
            long time = new Date().getTime();
            List<LabCronJob> jobs = new ArrayList<>();
//            jobs.add(new LabCronJob("lab_items_relev_basis", "1", " items_id='" + itemsId + "'",time, 1));
//            jobs.add(new LabCronJob("lab_items_basis_index", "2", " items_id='" + itemsId + "'", time, 2));
//            jobs.add(new LabCronJob("lab_items_relev_basis", "0", time, 3));
//            jobs.add(new LabCronJob("lab_items_basis_index", "0", time, 4));
            labCronJobService.saveBatch(jobs);
        }
        return Result.OK("保存成功!");
    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "项目与标准关联表-通过id删除")
    @ApiOperation(value = "项目与标准关联表-通过id删除", notes = "项目与标准关联表-通过id删除")
//	@RequiresPermissions("labItemsRelevBasis:lab_items_relev_basis:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        labItemsRelevBasisService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "项目与标准关联表-批量删除")
    @ApiOperation(value = "项目与标准关联表-批量删除", notes = "项目与标准关联表-批量删除")
//	@RequiresPermissions("labItemsRelevBasis:lab_items_relev_basis:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.labItemsRelevBasisService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "项目与标准关联表-通过id查询")
    @ApiOperation(value = "项目与标准关联表-通过id查询", notes = "项目与标准关联表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<LabItemsRelevBasis> queryById(@RequestParam(name = "id", required = true) String id) {
        LabItemsRelevBasis labItemsRelevBasis = labItemsRelevBasisService.getById(id);
        if (labItemsRelevBasis == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(labItemsRelevBasis);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param labItemsRelevBasis
     */
//    @RequiresPermissions("labItemsRelevBasis:lab_items_relev_basis:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LabItemsRelevBasis labItemsRelevBasis) {
        return super.exportXls(request, labItemsRelevBasis, LabItemsRelevBasis.class, "项目与标准关联表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
//    @RequiresPermissions("labItemsRelevBasis:lab_items_relev_basis:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LabItemsRelevBasis.class);
    }

}
